package com.kvest.scanrecognizer.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.kvest.scanrecognizer.R;
import com.kvest.scanrecognizer.utility.Utils;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 07.09.13
 * Time: 15:17
 * To change this template use File | Settings | File Templates.
 */
public class MainFragment extends Fragment {
    private OnButtonsClickListener onButtonsClickListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_fragment, container, false);

        view.findViewById(R.id.take_picture).setEnabled(Utils.isIntentAvailable(getActivity(), MediaStore.ACTION_IMAGE_CAPTURE));
        view.findViewById(R.id.take_picture).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onButtonsClickListener != null) {
                    onButtonsClickListener.onTakePictureButtonClick();
                }
            }
        });
        view.findViewById(R.id.select_picture).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onButtonsClickListener != null) {
                    onButtonsClickListener.onSelectPictureButtonClick();
                }
            }
        });
        view.findViewById(R.id.show_manager).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onButtonsClickListener != null) {
                    onButtonsClickListener.onManagerButtonClick();
                }
            }
        });

        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            onButtonsClickListener = (OnButtonsClickListener)activity;
        } catch (ClassCastException cce) {
            onButtonsClickListener = null;
        }
    }

    public interface OnButtonsClickListener {
        public void onTakePictureButtonClick();
        public void onSelectPictureButtonClick();
        public void onManagerButtonClick();
    }
}
